package com.replication.jdbc;

public class MysqlConnectionSelector extends AbstructConnectionSelector {
	static final String[] MASTER_ONLY_SQLS = { "SELECT LAST_INSERT_ID()" };
	static final String HEARTBEAT_SQL = "SELECT 0";

	private boolean switchHeartbeat = false;

	protected boolean isMasterOnly(String sql) {

		String upperSql = sql.trim().toUpperCase();

		for (String query : MASTER_ONLY_SQLS) {
			if (query.equalsIgnoreCase(upperSql)) {
				return true;
			}
		}

		if (HEARTBEAT_SQL.equals(upperSql)) {
			return heatbeatSelect();
		}

		return !isSelectFrom(sql);
	}

	private boolean heatbeatSelect() {
		if (this.switchHeartbeat)
			this.switchHeartbeat = false;
		else {
			this.switchHeartbeat = true;
		}
		return this.switchHeartbeat;
	}
}
